package array;

/**
 * @author le
 */
public class TotalTestUnit
{
    public static void main(String[] args)
    {
        try
        {
            int[] nums = {-2,1,2};
            System.out.println(search(nums,2));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static int search (int[] nums, int target) {
        // write code here
        int start = 0;
        int end = nums.length-1;
        int index = -1;
        while(start <= end){
            int mid = (start + end)/2;
            if (target > nums[mid]){
                start = mid +1;
            }else if (target < nums[mid]){
                end = mid - 1;
            }else{
                index = mid;
                break;
            }


        }
        while(index > 0){
            if (nums[index-1] != target){
                return index;
            }
            index--;
        }
        return index;
    }
}
